# 倒包
from urllib.request import urlopen, Request     # request 请求
import random
# from http.client import HTTPResponse

headers_list = [
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
                   "Chrome/98.0.4758.102 Safari/537.36 ",
    "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/"
    "537.36 Core/1.70.3883.400 QQBrowser/10.8.4559.400"
]


# 定义
def print_hi():
    # 请求的URL连接
    url = "http://59468.cn/"
    ua = random.choice(headers_list)    # 随机使用请求头
    request = Request(url=url)
    request.add_header('User-Agent', ua)
    print(ua)
    print(type(request))
    # git方法
    re = urlopen(request, timeout=20)
    # 打印
    print(type(re))
    print(request.get_header('User-Agent'))
    # print(re.closed)
    # with re:
    #     print(1, type(re))  # http.client.HTTPResponse 类文件对象
    #     print(2, re.status, re.reason)  # 状态
    #     print(3, re.geturl())   # 返回真正的URL
    #     print(4, re.info())  # headers
    #     print(5, re.read())  # 读取返回的内容
    #     print(6, re._method)
    # print(re.closed)


# 按间距中的绿色按钮以运行脚本。
if __name__ == '__main__':
    # 调用定义
    print_hi()
